<?php

namespace app\api\model;

use think\Model;
use think\facade\Db;

class Common extends Model
{
    protected $table;

    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 数据添加。
     * @param array $data
     * @return int|string
     */
    public function addData($data = [])
    {
        return Db::name($this->table)->insert($data);
    }

    /**
     * 添加多条记录
     */
    public function addInfo($array = [])
    {
        return Db::name($this->table)->insertAll($array);
    }

    /**
     * 修改数据。
     * @param array $where
     * @param array $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function saveData($where = [], $data = [])
    {
        return Db::name($this->table)->where($where)->update($data);
    }

    /**
     * 删除一条数据。
     * @param array $where
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delData($where = [])
    {
        return Db::name($this->table)->where($where)->delete();
    }

    /**
     * 删除多条数据
     *
     * @param string $field
     * @param [type] $condition
     * @return void
     */
    public function delMoreInfo($field = 'id', $condition)
    {
        return Db::name($this->table)->whereIn($field,$condition)->delete();
    }

    /**
     *获取一条数据。
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($where = [], $field = "*", $group = "", $order = [])
    {
        $res = Db::name($this->table)->where($where)->order($order)->field($field)->group($group)->find();
        return $res;
    }

    /**
     * 获取某一字段
     * @param $where
     * @param $field
     * @param $group
     * @param $order
     * @return array|mixed|Db|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
//    public function getValue($where = [], $field = "*", $group = "", $order = [])
//    {
//        $res = Db::name($this->table)->where($where)->value($field);
//        return $res;
//    }

    /**
     * 获取某一字段
     * @param $where
     * @param $field
     * @param $group
     * @param $order
     * @param $limit
     * @return array
     */
    public function getOneValue($where = [], $field = "*", $group = "", $order = [],$limit=[]){
        if(empty($limit)){
            return Db::name($this->table)->where($where)->order($order)->group($group)->column($field);
        }else{
         return Db::name($this->table)->where($where)->order($order)->group($group)->limit($limit[0],$limit[1])->column($field);
        }
    }
    /**
     * 获取数据。
     * @param array $where
     * @param string $field
     * @param string $group
     * @param array $order
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllData($where = [], $field = "*", $group = "" , $order = [],$limit =[])
    {
        if(empty($limit)) {
            $res = Db::name($this->table)->where($where)->order($order)->field($field)->group($group)->select();
        }else{
            $res = Db::name($this->table)->where($where)->order($order)->field($field)->group($group)->limit($limit[0],$limit[1])->select();
        }
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

    /**
     * 获取列表
     * @param string $field
     * @param array $order
     * @param string $group
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDataList($field='*', $order = [], $group = '')
    {
        $res = Db::name($this->table)->field($field)->order($order)->group($group)->select();
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

    /**
     *检测指定条件的个数
     * @param array $where
     * @return float|string
     */
    public function checkNum($where = [])
    {
        return Db::name($this->table)->where($where)->count();
    }
    public function checkInNum($searchwhere = [],$where='',$condition='')
    {
        return Db::name($this->table)->where($searchwhere)->whereIn($where,$condition)->count();
    }

    /**指定条件的数量
     * @param $where
     * @param $field
     * @return float
     */
    public function getSum($where = [],$field='')
    {
        return Db::name($this->table)->where($where)->sum($field);
    }

    /**
     * 获取whereNotIn的数据
     * @param string $where
     * @param string $condition
     * @param string $field
     * @param array $order
     * @param string $group
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function whereNotInInfo($searchWhere=[],$where = '', $condition = '', $field = '*', $order = [], $group = '')
    {
        $res = Db::name($this->table)->where($searchWhere)->whereNotIn($where,$condition)->field($field)->order($order)->group($group)->select();
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

    /**
     * 获取whereIn的数据
     * @param string $where
     * @param string $condition
     * @param string $field
     * @param array $order
     * @param string $group
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function whereInInfo($searchWhere=[],$where = '', $condition = '', $field = '*', $order = [], $group = '',$limit=[])
    {
        if(empty($limit) === FALSE){
            $res = Db::name($this->table)->where($searchWhere)->whereIn($where,$condition)->field($field)->order($order)->group($group)->limit($limit[0],$limit[1])->select();
        }else{
            $res = Db::name($this->table)->where($searchWhere)->whereIn($where,$condition)->field($field)->order($order)->group($group)->select();
        }
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

    /**
     * 指定条件下whereIn的数据
     * @param array $where
     * @param string $val
     * @param string $condition
     * @param string $field
     * @param array $order
     * @param string $group
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function whereInData($where = [], $val = '', $condition = '', $field = '*', $order = [], $group = '')
    {
        $res = Db::name($this->table)->where($where)->whereIn($val,$condition)->field($field)->order($order)->group($group)->select();
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

    /**
     * 获取某条件下最新插入的一条数据的ID值
     * @param array $where
     * @return string
     */
    public function getLastID($where = [])
    {
        return Db::name($this->table)->where($where)->getLastInsID();
    }

    /**
     * 添加一条记录并获取其ID
     * @param $data
     * @return int|string
     */
    public function getNewInsID($data)
    {
        return Db::name($this->table)->insertGetId($data);
    }

    /**
     * 某字段个数加n
     * @param array $where
     * @param string $field
     * @param int $num
     * @return int|true
     * @throws \think\Exception
     */
    public function incNum($where = [], $field, $num = 1)
    {
        return Db::name($this->table)->where($where)->inc($field,$num)->save();
    }

    /**
     * 某字段个数是减n
     * @param array $where
     * @param string $field
     * @param int $num
     * @return int|true
     * @throws \think\Exception
     */
    public function decNum($where = [], $field = '', $num = 1)
    {
        return Db::name($this->table)->where($where)->dec($field,$num)->save();
    }

    /**
     * whereIn下修改多条记录
     *
     * @param string $field
     * @param array $condition
     * @param array $data
     * @return array
     */
    public function whereInUpdate($field = 'id',$condition = [], $data = [])
    {
        return Db::name($this->table)->whereIn($field,$condition)->update($data);
    }

    /**
     * whereOr查询多条数据
     *
     * @param array $where
     * @param array $whereOr
     * @param string $field
     * @param array $order
     * @param string $group
     * @return array
     */
    public function whereOrInfo($where=[],$whereOr=[],$field='*',$order=[],$group='')
    {
        $res = Db::name($this->table)->where($where)->whereOr($whereOr)->field($field)->order($order)->group($group)->select();
        if(empty($res) === FALSE){
            return $res->toArray();
        }else{
            return $res;
        }
    }

}

